//Activity to set the schedule times

package g1.thermostat;

import java.util.Arrays;
import java.util.HashMap;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class T3Activity extends Activity {
	
	static final int TIME_DIALOG_ID = 0;
	
	Spinner spinner;
	
	Button bDelD1;
	Button bDelD2;
	Button bDelD3;
	Button bDelD4;
	Button bDelD5;
	Button bDelN1;
	Button bDelN2;
	Button bDelN3;
	Button bDelN4;
	Button bDelN5;
	
	Button btnCancel3;
	Button btnSave3;
	Button bAddDay;
	Button bAddNight;
	
	int[] dayList;
	int[] nightList;
	int dayChanges;
	int nightChanges;
	int daySlots;
	int nightSlots;
	String dayToday;
	String state;

	HashMap <String, DayNightTimes> weekLists;
	
	public void OnResume() {
		super.onResume();
		getSchedule(dayToday);
		showOnScreen();
	}
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        weekLists = new HashMap<String,DayNightTimes>();
        weekLists = ((Variables)getApplication()).getWeekSchedule();
        setContentView(R.layout.layout3);
        
        bDelD1 = (Button) findViewById(R.id.buttonDelD1);
		bDelD2 = (Button) findViewById(R.id.buttonDelD2);
		bDelD3 = (Button) findViewById(R.id.buttonDelD3);
		bDelD4 = (Button) findViewById(R.id.buttonDelD4);
		bDelD5 = (Button) findViewById(R.id.buttonDelD5);
		bDelN1 = (Button) findViewById(R.id.buttonDelN1);
		bDelN2 = (Button) findViewById(R.id.buttonDelN2);
		bDelN3 = (Button) findViewById(R.id.buttonDelN3);
		bDelN4 = (Button) findViewById(R.id.buttonDelN4);
		bDelN5 = (Button) findViewById(R.id.buttonDelN5);
		btnCancel3 = (Button) findViewById(R.id.btnCancel3);
		btnSave3 = (Button) findViewById(R.id.btnSave3);
		bAddDay = (Button) findViewById(R.id.buttonAddDay);
		bAddNight = (Button) findViewById(R.id.buttonAddNight);
		
        addListenerOnButton();
        addDelButtons();
        dayToday = "monday";
        getSchedule(dayToday);
        
        state = "";
        
        btnCancel3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				finish();
			}	
		});
		
		btnSave3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				saveDay();
				((Variables)getApplication()).setWeekSchedule(weekLists);
				finish();
			}	
		});
		
		bAddDay.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				state = "day";
				showDialog(TIME_DIALOG_ID);
			}	
		});
		
		bAddNight.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				state = "night";
				showDialog(TIME_DIALOG_ID);
			}	
		});
		
        
	}
	
	public void addListenerOnButton() {
		 
		spinner = (Spinner) findViewById(R.id.spinner1);
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View v,int pos, long id) {
            	saveDay();
            	dayToday = String.valueOf(spinner.getSelectedItem());
                getSchedule(dayToday);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // Nothing
            }
        });
	}
	
	public void addDelButtons() {
		
		
		bDelD1.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = dayList[0];
				delFromList("day", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelD2.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = dayList[1];
				delFromList("day", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelD3.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = dayList[2];
				delFromList("day", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelD4.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = dayList[3];
				delFromList("day", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelD5.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = dayList[4];
				delFromList("day", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelN1.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = nightList[1];
				delFromList("night", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelN2.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = nightList[2];
				delFromList("night", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelN3.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = nightList[3];
				delFromList("night", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelN4.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = nightList[4];
				delFromList("night", deltime);
				showOnScreen();
			  }
		 
		});
		
		bDelN5.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) {
				int deltime = nightList[5];
				delFromList("night", deltime);
				showOnScreen();
			  }
		 
		});
		
	}
	
	public void getSchedule(String day) {
		DayNightTimes derpy = new DayNightTimes();
		derpy = weekLists.get(day);
		dayList = derpy.day;
		nightList = derpy.night;
		dayChanges = derpy.dayChanges;
		nightChanges = derpy.nightChanges;
		showOnScreen();
	}
	
	public void showOnScreen() {
		
		TextView textD1;
		TextView textD2;
		TextView textD3;
		TextView textD4;
		TextView textD5;
		TextView textDayCh;
		TextView textDaySl;
		
		TextView textN1;
		TextView textN2;
		TextView textN3;
		TextView textN4;
		TextView textN5;
		TextView textNightCh;
		TextView textNightSl;
		
		textD1 = (TextView)findViewById(R.id.textD1);
		textD2 = (TextView)findViewById(R.id.textD2);
		textD3 = (TextView)findViewById(R.id.textD3);
		textD4 = (TextView)findViewById(R.id.textD4);
		textD5 = (TextView)findViewById(R.id.textD5);
		
		textN1 = (TextView)findViewById(R.id.textN1);
		textN2 = (TextView)findViewById(R.id.textN2);
		textN3 = (TextView)findViewById(R.id.textN3);
		textN4 = (TextView)findViewById(R.id.textN4);
		textN5 = (TextView)findViewById(R.id.textN5);
		
		daySlots = 5;
		nightSlots = 5;
		
		if (dayList[0]< 2400) {
			String x = (dayList[0]/100)+"";
			String y = (dayList[0]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textD1.setText(x+":"+y);
			daySlots--;
			bDelD1.setEnabled(true);
		}
		
		else {
			textD1.setText("Not Set");
			bDelD1.setEnabled(false);
		}
		
		if (dayList[1]< 2400) {
			String x = (dayList[1]/100)+"";
			String y = (dayList[1]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textD2.setText(x+":"+y);
			daySlots--;
			bDelD2.setEnabled(true);
		}
		
		else {
			textD2.setText("Not Set");
			bDelD2.setEnabled(false);
		}
		
		if (dayList[2]< 2400) {
			String x = (dayList[2]/100)+"";
			String y = (dayList[2]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textD3.setText(x+":"+y);
			daySlots--;
			bDelD3.setEnabled(true);
		}
		
		else {
			textD3.setText("Not Set");
			bDelD3.setEnabled(false);
		}
		
		if (dayList[3]< 2400) {
			String x = (dayList[3]/100)+"";
			String y = (dayList[3]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textD4.setText(x+":"+y);
			daySlots--;
			bDelD4.setEnabled(true);
		}
		
		else {
			textD4.setText("Not Set");
			bDelD4.setEnabled(false);
		}
		
		if (dayList[4]< 2400) {
			String x = (dayList[4]/100)+"";
			String y = (dayList[4]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textD5.setText(x+":"+y);
			daySlots--;
			bDelD5.setEnabled(true);
		}
		
		else {
			textD5.setText("Not Set");
			bDelD5.setEnabled(false);
		}
		
		
		
		if (nightList[1]< 2400) {
			String x = (nightList[1]/100)+"";
			String y = (nightList[1]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textN1.setText(x+":"+y);
			nightSlots--;
			bDelN1.setEnabled(true);
		}
		
		else {
			textN1.setText("Not Set");
			bDelN1.setEnabled(false);
		}
		
		if (nightList[2]< 2400) {
			String x = (nightList[2]/100)+"";
			String y = (nightList[2]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textN2.setText(x+":"+y);
			nightSlots--;
			bDelN2.setEnabled(true);
		}
		
		else {
			textN2.setText("Not Set");
			bDelN2.setEnabled(false);
		}
		
		if (nightList[3]< 2400) {
			String x = (nightList[3]/100)+"";
			String y = (nightList[3]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textN3.setText(x+":"+y);
			nightSlots--;
			bDelN3.setEnabled(true);
		}
		
		else {
			textN3.setText("Not Set");
			bDelN3.setEnabled(false);
		}
		
		if (nightList[4]< 2400) {
			String x = (nightList[4]/100)+"";
			String y = (nightList[4]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textN4.setText(x+":"+y);
			nightSlots--;
			bDelN4.setEnabled(true);
		}
		
		else {
			textN4.setText("Not Set");
			bDelN4.setEnabled(false);
		}
		
		if (nightList[5]< 2400) {
			String x = (nightList[5]/100)+"";
			String y = (nightList[5]%100)+"";
			
			if (x.length()==1) {
				x = "0"+x;
			}
			
			if (y.length()==1) {
				y = "0"+y;
			}
			
			textN5.setText(x+":"+y);
			nightSlots--;
			bDelN5.setEnabled(true);
		}
		
		else {
			textN5.setText("Not Set");
			bDelN5.setEnabled(false);
		}
		
		textDayCh = (TextView)findViewById(R.id.textDayCh);
		textDayCh.setText(dayChanges+" Changes-To-Day Set");
		
		textDaySl = (TextView)findViewById(R.id.textDaySl);
		textDaySl.setText(daySlots+" Time Slots Availible");
		
		textNightCh = (TextView)findViewById(R.id.textNightCh);
		textNightCh.setText(nightChanges+" Changes-To-Day Set");
		
		textNightSl = (TextView)findViewById(R.id.textNightSl);
		textNightSl.setText(nightSlots+" Time Slots Availible");
		
	}
	
	public void addToList(String state, int timei) {
		if (state.equals("day")) {
			for (int i=0;i<dayList.length;i++) {
				if (dayList[i] == 9999) {
					dayList[i] = timei;
					dayChanges++;
					i=dayList.length;
				}
			}
			Arrays.sort(dayList);
		}
		
		if (state.equals("night")) {
			for (int i=0;i<nightList.length;i++) {
				if (nightList[i] == 9999) {
					nightList[i] = timei;
					nightChanges++;
					i=nightList.length;
				}
			}
			Arrays.sort(nightList);
		}
		showOnScreen();
	}
	
	public void delFromList(String state, int timei) {
		if (state.equals("day")) {
			for (int i=0;i<dayList.length;i++) {
				if (dayList[i] == timei) {
					dayList[i] = 9999;
					dayChanges--;
					i=dayList.length;
				}
			}
			Arrays.sort(dayList);
		}
		
		if (state.equals("night")) {
			for (int i=0;i<nightList.length;i++) {
				if (nightList[i] == timei) {
					nightList[i] = 9999;
					nightChanges--;
					i=nightList.length;
				}
			}
			Arrays.sort(nightList);
		}
		showOnScreen();
	}
	
	public void saveDay() {
		DayNightTimes derp = new DayNightTimes();
    	derp.day = dayList;
    	derp.night = nightList;
    	derp.dayChanges = dayChanges;
    	derp.nightChanges = nightChanges;
    	weekLists.put(dayToday, derp);
	}
	
	public String checkTimeInList(int timea) {
		String oal ="false";
		for (int h=0;h<dayList.length;h++) {
			if (dayList[h] == timea) {
				oal = "day";
				h = dayList.length;
			}
		}
		
		for (int h=0;h<nightList.length;h++) {
			if (nightList[h] == timea) {
				oal = "night";
				h = nightList.length;
			}
		}
		
		if (timea == 0 && state.equals("day")) {
			oal = "false";
		}
		
		if (timea == 0 && state.equals("night")) {
			oal = "set";
		}
		return oal;
	}
	
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
				if (hourOfDay == 24) {
					hourOfDay = 0;
				}
		        setThisTime(state, hourOfDay, minutes);
		    }
	};
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case TIME_DIALOG_ID:
				return new TimePickerDialog(this, mTimeSetListener, 0, 0, true);
			}
	    return null;
	}
	
	public void setThisTime(String day,int hour,int minute) {
		int newTime = hour*100+minute;
		Log.v("PassedTimeDialog",newTime+"");
		if (checkTimeInList(newTime).equals("false") && day.equals("day")) {
			addToList("day", newTime);
			
			showOnScreen();
		} else if (checkTimeInList(newTime).equals("false") && day.equals("night")) {
			addToList("night", newTime);
			showOnScreen();
		} else {
			String s = checkTimeInList(newTime);
			String ce = "";
			if (s.equals("day")) {
				String x = (newTime/100)+"";
				String y = (newTime%100)+"";
				
				if (x.length()==1) {
					x = "0"+x;
				}
				
				if (y.length()==1) {
					y = "0"+y;
				}
				ce = x+":"+y + " is already set in day!";
			} else if (s.equals("night")) {
				String x = (newTime/100)+"";
				String y = (newTime%100)+"";
				
				if (x.length()==1) {
					x = "0"+x;
				}
				
				if (y.length()==1) {
					y = "0"+y;
				}
				ce = x+":"+y + " is already set in night!";
			} else if (s.equals("set")) {
				ce = "00:00 is the standard switch to night.";
			}
			
			Toast toast=Toast.makeText(this, ce, 2000);
			toast.show();
		}
	}
}